﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CosyTrade.Financial;
using CosyTrade.Organization;
using CosyTrade.UI;
using DevExpress.XtraEditors.Controls;
using CosyTrade.ClientForm.Properties;

namespace CosyTrade.ClientForm.Financial
{
    public partial class ExchangeRateSettingForm : FormBase
    {
        public ExchangeRateSettingForm()
        {
            InitializeComponent();
        }

        protected override void InitCommandBar()
        {
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("CreateExchangeRate", Resources.add, "新建汇率", true, true, true,
                                                               CreateExchangeRateCallback));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("Refresh", Resources.reload, "刷新", true, true, true,
                                                               RefreshData));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("Close", Resources.door_out, "关闭", true, true, true,
                                                               CloseCallback));
        }
        
        private void CreateExchangeRateCallback(object sender, EventArgs e)
        {
            ExchangeRateDetailForm detailForm = new ExchangeRateDetailForm();
            detailForm.FormClosed += new FormClosedEventHandler(detailForm_FormClosed);
            detailForm.Show();
        }

        void detailForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            RefreshData();
        }

        private void RefreshData(object sender, EventArgs e)
        {
            RefreshData();
        }  
      
        private void CloseCallback(object sender, EventArgs e)
        {
            Close();
        }

        private void ExchangeRateSettingForm_Load(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            IList<ExchangeRate> dataSource =
                Operation.FinancialSystem.GetExchangeRateList(GlobalInfo.CurrentCompany.PrimaryKey);
            bindingSource1.DataSource = dataSource;

            cbExchangeRateList.Properties.Items.Clear();
            foreach (ExchangeRate rate in dataSource)
            {
                string str = string.Format("第{0}次价钱 - {1}", rate.ExchangeIndex, rate.Exchange);
                ImageComboBoxItem item = new ImageComboBoxItem(str, rate);
                cbExchangeRateList.Properties.Items.Add(item);
                if (rate.CurrentRate)
                    cbExchangeRateList.SelectedItem = item;
            }
        }

        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Value as Company != null)
                e.DisplayText = ((Company) e.Value).Caption;
        }

        private void btnSetDefault_Click(object sender, EventArgs e)
        {

            ExchangeRate rate = cbExchangeRateList.EditValue as ExchangeRate;
            if (rate == null)
            {
                Functions.ShowMessageBox("请选择默认汇率");
                return;
            }
            Operation.FinancialSystem.SetDefaultExchangeRate(rate.PrimaryKey);
            Functions.ShowMessageBox("设置成功");
        }
    }
}
